package com.bsh.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.api.params.DtoUpgradeGrade;
import com.bsh.mybatis.model.User;
import com.bsh.repository.DaoPayment;
import com.bsh.service.ApiUpgradeService;
import com.bsh.service.UpgradeService;
import com.bsh.service.UserService;
import com.bsh.utils.ApiMessage;
@Service
@Transactional
public class ApiUpgradeServiceImpl implements ApiUpgradeService {

	@Autowired
	UserService servUser;
	@Autowired
	UpgradeService upgradeSer;
	@Autowired
	DaoPayment daoPay;

	@Override
	public ApiMessage gradeBalance(JSONObject dto) throws ApiException {
		ApiMessage msg = null;
		ApiUtils.checkUserNameToken(dto);
		User userInfo = servUser.getUserByName(dto.getString("userName"));
		if (userInfo == null) {
			msg = new ApiMessage("400", "用户不存在,错误");
			return msg;
		}
		if (userInfo.getLevel().equals(dto.getString("level"))) {
			msg = new ApiMessage("400", "已经是" + dto.getString("level") + "请不要重复操作");
			return msg;
		}
		if (dto.getString("level").equals("vip") && userInfo.getLevel().equals("gold")) {
			msg = new ApiMessage("400", "已经是" + userInfo.getLevel() + " 不用升级" + dto.getString("level"));
			return msg;
		}

		String ip = ApiUtils.objIsNotEmpty(dto.getString("req_ip")) ? dto.getString("req_ip") : dto.getString("ip");
		// 进service
		DtoUpgradeGrade result = upgradeSer.upgradeMemberBalance(userInfo.getName(), dto.getString("paypassword"),
				dto.getString("level"), ip);

		if (result.getRes() > 0) {
			msg = new ApiMessage("200", "升级成功");
		} else {
			String message = "升级操作失败";
			if (result.getMessage() != null) {
				message = result.getMessage();
			}
			if (result.getRes() == -11) {
				message = message + result.getRes();
			}
			msg = new ApiMessage("400", message);
		}
		return msg;
	}

	@Override
	public ApiMessage gradeOnline(JSONObject dto) throws ApiException {
		ApiMessage msg = null;
//		ApiUtils.checkUserNameToken(dto);
//		User userInfo = servUser.getUserByName(dto.getString("userName"));
//		if (userInfo == null) {
//			msg = new ApiMessage("400", "用户不存在,错误");
//			return msg;
//		}
//		if (userInfo.getLevel().equals(dto.getString("level"))) {
//			msg = new ApiMessage("400", "已经是" + dto.getString("level") + "请不要重复操作");
//			return msg;
//		}
//		if (dto.getString("level").equals("vip") && userInfo.getLevel().equals("gold")) {
//			msg = new ApiMessage("400", "已经是" + userInfo.getLevel() + " 不用升级" + dto.getString("level"));
//			return msg;
//		}
//		Payment payment = daoPay.getPaymentByClassName(dto.getString("paymentType"));
//		if (payment == null) {
//			msg = new ApiMessage("400", "支付方式不正确 ");
//			return msg;
//		}
//
//		String ip = ApiUtils.objIsNotEmpty(dto.getString("req_ip")) ? dto.getString("req_ip") : dto.getString("ip");
//		// 进service
//		DtoUpgradeGrade result = upgradeSer.upgradeMemberOnline(userInfo.getName(), dto.getString("paypassword"),
//				dto.getString("level"), ip, dto.getString("paymentType"));
//
//		if (result.getRes() > 0) {
//			msg = new ApiMessage("200", "提交成功,等待付款");
//			Map<String, Object> data = new HashMap<String, Object>();
//			data.put("token", dto.getString("token"));
//			data.put("money", result.getMoney());
//			data.put("sn", result.getSn());
//			data.put("desc", result.getDesc());
//			msg.setData(Des3.encResponse(data));
//		} else {
//			String message = "升级操作失败";
//			if (result.getMessage() != null) {
//				message = result.getMessage();
//			}
//			if (result.getRes() == -11) {
//				message = message + result.getRes();
//			}
//			msg = new ApiMessage("400", message);
//		}
		msg=new ApiMessage("400","会员升级请使用【余额支付】");
		return msg;
		
	}

}
